<template>
    <div class="User-container content-wrapper">

        <div class="filter-wrapper">
            <el-form ref="filterForm" :model="listQuery" :inline="true">
                <el-form-item prop="title" size="medium">
                    <el-input v-model="listQuery.title" placeholder="标题"></el-input>
                </el-form-item>

                <el-form-item prop="status" size="medium">
                    <el-select v-model="listQuery.type" placeholder="类型" clearable style="width: 100px">
                        <el-option
                                v-for="item in typeOptions"
                                :key="item.key"
                                :label="item.label"
                                :value="item.key">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item prop="orderBy" size="medium">
                    <el-select v-model="listQuery.orderBy" placeholder="排序" style="width: 120px">
                        <el-option
                                v-for="item in sortOptions"
                                :key="item.key"
                                :label="item.label"
                                :value="item.key">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-button type="primary" size="medium" icon="el-icon-search" @click="handleFilter">搜索</el-button>
                <el-button type="primary" size="medium" icon="el-icon-mars-reset" @click="handleResetFilter">重置</el-button>
                <el-button type="primary" size="medium" icon="el-icon-plus" @click="handlerSendMessage">发送消息</el-button>
                <el-button type="primary" size="medium" icon="el-icon-plus" @click="handlerSyncMaterial">同步素材列表</el-button>
                <el-button type="danger" size="medium" icon="el-icon-delete" @click="handleBatchDelete">批量删除</el-button>
            </el-form>

        </div>

        <el-table
                :data="list"
                border fit
                highlight-current-row
                v-loading="listLoading"
                element-loading-text="给我一点时间"
                @selection-change="handleSelectionChange">

            <el-table-column
                    type="selection"
                    width="55" align="center">
            </el-table-column>
            <el-table-column type="index" width="55" align="center"></el-table-column>

            <el-table-column label="类型" width="100px" align="center">
                <template slot-scope="scope">
                    <el-tag type="success" v-if="scope.row.type === 'image'">图片</el-tag>
                    <el-tag type="success" v-else-if="scope.row.type === 'video'">视频</el-tag>
                    <el-tag type="success" v-else-if="scope.row.type === 'voice'">语音</el-tag>
                    <el-tag type="danger" v-else>图文</el-tag>
                </template>
            </el-table-column>

            <el-table-column label="标题" width="300px" align="center">
                <template slot-scope="scope"> {{scope.row.title}} </template>
            </el-table-column>

            <el-table-column label="素材MediaId" width="300px" align="center">
                <template slot-scope="scope"> {{scope.row.mediaId}} </template>
            </el-table-column>

            <el-table-column label="图片" width="200" align="center">
                <template slot-scope="scope">
                    <template v-if="scope.row.type === 'image'">
                        <a :href="scope.row.url" target="_blank">
                            <img :src="scope.row.url" alt="" style="height: 100px">
                        </a>
                    </template>
                </template>
            </el-table-column>

            <el-table-column label="图文列表" min-width="300px" align="center">
                <template slot-scope="scope">
                    <template v-if="scope.row.type === 'news'">
                        <span v-html="parseItem(scope.row.subNews)"></span>
                    </template>
                </template>
            </el-table-column>

            <el-table-column label="操作" width="180px" align="center">
                <template slot-scope="scope">
                    <el-button size="small" type="danger" icon="delete" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <div v-show="!listLoading" class="pagination-container">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="listQuery.pageNum"
                    :page-sizes="[10, 20, 30, 50]"
                    :page-size="listQuery.pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

    </div>
</template>

<script>
    import { created, updated, deleted, deletedBatch, selectOne, selectList, syncMaterial } from '@/api/weixin/material'

    const sortOptions = [{label: '升序', key: 'ctime asc'}, {label: '降序', key: 'ctime desc'}]
    const typeOptions = [{label: '图文', key: 'news'}, {label: '图片', key: 'image'}, {label: '视频', key: 'video'}, {label: '语音', key: 'voice'}]

    export default {
        name: 'Article',
        data() {
            return {
                list: [],
                total: null,
                listLoading: false,
                multipleSelection: [],
                listQuery: {
                    pageNum: 1,
                    pageSize: 10,
                    title: null,
                    type: null,
                    orderBy: 'ctime desc'
                },
                typeOptions,
                sortOptions,
                form: {
                },
                rules: {
                }
            }
        },
        created() {
            this.getTableData()
        },
        methods: {
            parseItem(items) {
                let subitem = ''
                var json = JSON.parse(items)
                for(let item of json) {
                    var arr = item.split(',')
                    subitem += '<a href="'+arr[1]+'" target="_blank">'+arr[0]+'</a><br>'
                }
                return subitem;
            },
            handlerSendMessage() {
                this.$router.push({path: 'form'})
            },
            handlerSyncMaterial() {
                this.listLoading = true
                syncMaterial().then(response => {
                    this.getTableData()
                })
            },

            handleFilter() {//条件过滤
                this.listQuery.pageNum = 1
                this.getTableData()
            },
            handleResetFilter() {//重置过滤条件
                this.$refs.filterForm.resetFields()
                this.getTableData()
            },
            handleDialogClose() {//关闭窗口回调函数
                this.resetForm()
            },
            handleSelectionChange(val) {//多选
                this.multipleSelection = val;
            },
            handleSizeChange(val) {
                this.listQuery.pageSize = val
                this.getTableData()
            },
            handleCurrentChange(val) {
                this.listQuery.pageNum = val
                this.getTableData()
            },

            handleDelete(row) {//删除数据
                this.$confirm('确定要删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleted(row.id).then(response => {
                        if(response.data.status) {
                            this.$notify({
                                title: '成功', message: response.data.message,
                                type: 'success', duration: 2000
                            })
                            this.getTableData()
                        } else {
                            this.$notify({
                                title: '发生错误', message: response.data.message,
                                type: 'error', duration: 2000
                            })
                        }
                    })
                }).catch(() => {
                    this.$message({ type: 'warning', message: '已取消删除' })
                })
            },
            handleBatchDelete() {//批量删除数据
                this.$confirm('确定要删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    if(this.multipleSelection.length > 0) {
                        let ids = []
                        for(let item of this.multipleSelection) {
                            ids.push(item.id)
                        }
                        deletedBatch({ids: ids.join(',')}).then(response => {
                            if(response.data.status) {
                                this.$notify({
                                    title: '成功', message: response.data.message,
                                    type: 'success', duration: 2000
                                })
                                this.getTableData()
                            } else {
                                this.$notify({
                                    title: '发生错误', message: response.data.message,
                                    type: 'error', duration: 2000
                                })
                            }
                        })

                    } else {
                        this.$notify({
                            title: '提示', message: '请选择需要删除的记录',
                            type: 'warning', duration: 2000
                        })
                    }
                }).catch(() => {
                    this.$message({ type: 'info', message: '已取消删除' })
                })
            },
            getTableData() {
                this.listLoading = true
                selectList(this.listQuery).then(response => {
                    this.list = response.data.list
                    this.total = response.data.total
                    this.listLoading = false
                }).catch(error => {
                    console.log(error)
                    this.listLoading = false
                })
            }
        }
    }
</script>

<!-- 局部样式 -->
<style rel="stylesheet/scss" lang="scss" scoped>

</style>
